package com.text.dao;

import com.text.entity.Category;
import com.text.entity.Goods;
import org.apache.ibatis.annotations.*;

import java.util.ArrayList;

@Mapper
public interface GoodsDao {
    @Results({
            @Result(column = "category_id",property = "categoryId"),
            @Result(column = "sale_quantity",property = "saleQuantity"),
            @Result(column = "category_id",property="category",javaType = Category.class,
                    one = @One(select = "com.text.dao.CategoryDao.queryBytID")
            )
    })
    @Select("Select * from goods")
    ArrayList<Goods> getGoodsList();

    @Delete("delete from goods where id = #{id}")
    int deleteById(Integer id);

    @Insert("INSERT INTO goods" +
            "(category_id,code,name,price,quantity,sale_quantity,addtime,hot,image)" +
            "VALUES(" +
            "#{goods.categoryId}" +
            ",#{goods.code}" +
            ",#{goods.name}" +
            ",#{goods.price}" +
            ",#{goods.quantity}" +
            ",0" +
            ",now()" +
            ",#{goods.hot}" +
            ",#{goods.image}" +
            ")")
    @Options(useGeneratedKeys = true, keyProperty = "goods.id")
    int insert(@Param("goods") Goods goods);
    @Results({
            @Result( column = "category_id",property = "categoryId"),
            @Result( column = "sale_quantity",property = "saleQuantity"),
    })
    @Select("select * from goods where id = #{id}")
    Goods hyqById(Integer id);

    @Update("update goods SET " +
            " category_id = #{edit.categoryId}" +
            ",code = #{edit.code}" +
            ",name = #{edit.name}" +
            ",price = #{edit.price}" +
            ",quantity = #{edit.quantity}" +
            ",hot = #{edit.hot}" +
            ",image = #{edit.image}" +
            "  where id = #{edit.id}")
    int hyqUpDate(@Param("edit") Goods edit);
}






